package com.cnd.scm.rabbitmq.common;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * @version V1.0
 * @ClassName: SimpleMessageSender
 * @Description:
 * @author: 李启联
 * @date: 2021-03-18 18:08
 * @Copyright: Copyright (c), 2021
 */
@Service
public class SimpleMessageSender implements MessageSender {

    private RabbitTemplate rabbitTemplate;

    private Map<String, MessageLog> map = new ConcurrentSkipListMap<>();

    @Override
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }


    /**
     * 向发送消息容器增加要发送的消息
     *
     * @param messageLog
     */
    @Override
    public void storeMessage(MessageLog messageLog) {
        //map.putIfAbsent(messageLog.getId(), messageLog);
    }

    /**
     * 更新保存在容器中投递消息的状态
     *
     * @param status 消息状态
     * @param msgId  消息ID
     */
    @Override
    public void updateStatus(MessageStatusEnum status, String msgId) {

    }

    /**
     * 删除发送容器中的一条消息
     *
     * @param id
     */
    @Override
    public void delMessage(String id) {

    }

    /**
     * 发送消息
     *
     * @param message
     * @return
     */
    @Override
    public DetailResponse send(Object message) {
        String id = UUID.randomUUID().toString().replace("-","").toUpperCase();
        return null;//send(new MessageLog(id, message,rabbitTemplate.getExchange(),rabbitTemplate.getRoutingKey()));
    }

    /**
     * 发送消息
     *
     * @param messageLog
     * @return
     */
    @Override
    public DetailResponse send(MessageLog messageLog) {
        try {
            storeMessage(messageLog);
            //rabbitTemplate.correlationConvertAndSend(messageLog.getMessage(), new CorrelationData(String.valueOf(messageLog.getId())));
            //rabbitTemplate.convertAndSend(messageWithTime.getRoutingKey(),messageWithTime.getMessage(),new CorrelationData(String.valueOf(messageWithTime.getId())));
        } catch (Exception e) {
            return new DetailResponse(false, "", "");
        } finally {
            writeMessageToLocal();
        }
        return new DetailResponse(true, "", "");
    }

    /**
     * 开启重试发送机制
     */
    @Override
    public void retry() {

    }

    private void  writeMessageToLocal(){
        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream("d:\\senderMessage.txt"));
            for (Map.Entry<String, MessageLog> entry : map.entrySet()) {
                out.writeObject(entry.getValue().toString());
            }

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());

        } finally{
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}